<?php
defined('APP_PATH') or die ('Unauthorized access');

/**
 * 控制器
 * @author windpurple
 * @version 1.0
 */
include_once(LIB_PATH.'/Error.php');
include_once(LIB_PATH.'/Router.php');
include_once(LIB_PATH.'/View.php');
include_once(LIB_PATH.'/Load.php');

if(isset($config['session']) && $config['session']['save_handler'] == 'database' ) include_once(LIB_PATH.'/Session.php');

class Controller
{
	protected $_router = NULL;
	protected $view = NULL;
	static private $_instance = NULL;
	
	private function __construct()
	{
		$this->_router = Router::getInstance();
	}
	
	private function __clone()
	{
		
	}
	
	static function getInstance()
	{
		if(is_null(self::$_instance) || !isset(self::$_instance)){
			$class = __CLASS__;
			self::$_instance = new $class;
		}
		return self::$_instance;
	}
	
	final public function run()
	{
		global $config;

		$router = $this->_router->get();	//获取路由
		
		//获取文件控制器路径
		if($config['multi_mode']){
			$controller_file = PRO_PATH.'/'.ucfirst($router['directory']).'/Controller/'.ucfirst($router['controller']);
			$class_name = ucfirst($router['directory'].'_'.ucfirst($router['controller']).'Controller');
			$view_file = PRO_PATH.'/'.ucfirst($router['directory']).'/View/'.$router['controller'].'/'.$router['action'].'.'.$config['view_suffix'];
		}else{
			$controller_file = PRO_PATH.'/Controller/'.ucfirst($router['controller']);
			$class_name = ucfirst($router['controller']).'Controller';
			$view_file = PRO_PATH.'/View/'.$router['controller'].'/'.$router['action'].'.'.$config['view_suffix'];
		}
		
		$controller_file = $controller_file.'Controller.php';
		$router['action'] = empty($router['action']) ? 'index' : $router['action'];
		$controller_filename = str_replace(PRO_PATH.'/','',$controller_file);

		//检查文件是否存在
		if(!file_exists($controller_file)){
			Error::message($controller_filename.' 文件不存在');
		}
		
		require_once($controller_file);
		
		//检查类是否存在
		if(!class_exists($class_name)){
			Error::message($controller_filename.' 文件中 '.$class_name.' 类未定义');
		}
		
		$controller = new $class_name();


		//检查方法是否存在
		if(!method_exists($controller,$router['action'])){
			Error::message($controller_filename.' 文件中类 '.$class_name.'  之 '.$router['action'].' 方法不存在');
		}
		
		//在构造函数里面声明的view变量
		$init_view = $controller->view;	

		$controller->view = View::getInstance();
		if(!empty($init_view)){
			foreach($init_view as $k => $v){
				$controller->view->$k = $v;
			}
		}

		$controller->view->controller = $router['controller'];
		$controller->view->action = $router['action'];
		$controller->view->view_file = $view_file;
		$controller->view->view_filepath = str_replace($router['action'].'.'.$config['view_suffix'],'',$view_file); 
		$controller->view->view_suffix = $config['view_suffix'];
		$controller->$router['action']();
		
		//检查视图文件是否存在
		if(!file_exists($view_file)){
			Error::message($controller_filename.' 文件中类 '.$class_name.'  之 '.$router['controller'].'/'.$router['action'].'.'.$config['view_suffix'].' 视图文件不存在');
		}
		
		$controller->view->display($router['action'],false);
		
		unset($controller);
		
		

	}
	
	/**
	 * 跳转
	 * @param unknown_type $url
	 */
	public function redirect($url)
	{
		header("Location:{$url}");
		echo "<script type='text/javascript'>",
				"window.top.location.href='{$url}'",
				"</script>";
		exit;
	}
	
	/**
	 * 获取传值
	 * @param unknown_type $param
	 */
	public function request($param)
	{
		if(isset($_REQUEST)){
			return in_array($param,array_keys($_REQUEST)) ? $_REQUEST[$param] : false;
		}else{
			return false;
		}
	}
	
	

	
}


?>